from typing import List
from sortedcontainers import SortedList

"""
看的题解，滑动窗口
"""


class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        start = 0
        end = 0
        arr = SortedList()
        res = 0
        arr.add(nums[0])
        while start < len(nums):
            while arr[-1] - arr[0] <= limit:
                res = max(res, end - start + 1)
                print(arr)
                end += 1
                if end >= len(nums):
                    break
                arr.add(nums[end])
            arr.remove(nums[start])
            start += 1
        return res
